<?php
namespace app\index\controller;

use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\EnginModel;
use app\index\model\FlowLogModel;
use app\index\model\LegalModel;
use app\index\model\UserModel;
use app\index\service\LegalService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use think\Db;
use think\Exception;
use think\Request;

class Legal extends Common
{

    public function list_legal(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['le.deleted_user','=', 0];
        $where[] = ['le.comid','=',$this->userinfo['comid']];

        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['le.type','=', $param['type']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['le.year','like', '%'.$param['year'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['le.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['le.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['cause']) && !empty($param['cause'])){
            $where[] = ['le.cause','like', '%'.$param['cause'].'%'];
        }
        if(isset($param['she_unit']) && !empty($param['she_unit'])){
            $where[] = ['le.she_unit','=', $param['she_unit']];
        }
        if(isset($param['engin']) && !empty($param['engin'])){
            $where[] = ['engin.name','like', '%'.$param['engin'].'%'];
        }
        if(isset($param['xianger']) && !empty($param['xianger'])){
            $where[] = ['le.xianger','like', '%'.$param['xianger'].'%'];
        }
        if(isset($param['xiangtel']) && !empty($param['xiangtel'])){
            $where[] = ['le.xiangtel','like', '%'.$param['xiangtel'].'%'];
        }
        if(isset($param['gao_yuan']) && !empty($param['gao_yuan'])){
            $where[] = ['le.gao_yuan','like', '%'.$param['gao_yuan'].'%'];
        }
        if(isset($param['gao_bei']) && !empty($param['gao_bei'])){
            $where[] = ['le.gao_bei','like', '%'.$param['gao_bei'].'%'];
        }
        if(isset($param['gener']) && !empty($param['gener'])){
            $where[] = ['le.gener','like', '%'.$param['gener'].'%'];
        }
        if(isset($param['zhuang']) && !empty($param['zhuang'])){
            $where[] = ['le.zhuang','=', $param['zhuang']];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['le.state','=', $param['state']];
        }

        $data['type'] = (new LegalService())->get_legal_type();
        $data['is_type'] = isset($param['type']) ? $param['type'] : '';

        $data['zhuang'] = (new OptService())->get_base_info('fa_legal_zhuang');
        $data['is_zhuang'] = isset($param['zhuang']) ? $param['zhuang'] : '';

        $data['state'] = (new LegalService())->get_legal_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['she_unit'] = (new UserService())->get_chaninfo_all();;
        $data['is_she_unit'] = isset($param['she_unit']) ? $param['she_unit'] : '';

        $data['data'] = LegalModel::alias('le')
            ->field('le.*,engin.name as engin_name,she.name as she_unit_name')
            ->leftJoin('dept she','she.id=le.she_unit')
            ->leftJoin('engin engin','engin.id=le.engin')
            ->where($where)
            ->order('le.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['type_name'] = (new LegalService())->get_legal_type_name($item['type']);
                $item['zhuang'] = (new OptService())->get_base_showname('fa_legal_zhuang', $item['zhuang']);
                $item['state_name'] = (new LegalService())->get_legal_state_name($item['state']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('legal');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_legal(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['type'])){
                return outputJson(4000, '请选择案件类型');
            }
            if(empty($param['year'])){
                return outputJson(4000, '请选择案件年度');
            }
            if(empty($param['cause'])){
                return outputJson(4000, '请输入案由');
            }
            if(empty($param['she_unit'])){
                return outputJson(4000, '请选择涉案单位');
            }
            if(empty($param['gao_yuan'])){
                return outputJson(4000, '请输入原告');
            }
            if(empty($param['gao_bei'])){
                return outputJson(4000, '请输入被告');
            }
            if(empty($param['biaofee'])){
                return outputJson(4000, '请输入诉讼标的');
            }
            if(empty($param['zhuang'])){
                return outputJson(4000, '请选择案件状态');
            }
            if(!isset($param['jindu'])){
                return outputJson(4000, '请选择案件进度');
            }

            if(!in_array(3, $param['jindu'])){
                $param['bei_su'] = '';
                $param['bei_ce'] = '';
            }
            if(!in_array(6, $param['jindu'])){
                $param['li_dt'] = '';
                $param['li_shou'] = '';
                $param['li_sufee'] = '';
                $param['li_miao'] = '';
            }
            if(!in_array(9, $param['jindu'])){
                $param['yi_number'] = '';
                $param['yi_shen'] = '';
                $param['yi_dt'] = '';
                $param['yi_sufee'] = '';
                $param['yi_an'] = '';
                $param['yi_pan'] = '';
            }
            if(!in_array(12, $param['jindu'])){
                $param['er_number'] = '';
                $param['er_shen'] = '';
                $param['er_dt'] = '';
                $param['er_sufee'] = '';
                $param['er_an'] = '';
                $param['er_pan'] = '';
            }
            if(!in_array(15, $param['jindu'])){
                $param['zhi_number'] = '';
                $param['zhi_yuan'] = '';
                $param['zhi_dt'] = '';
                $param['zhi_shuo'] = '';
            }
            if(!in_array(18, $param['jindu'])){
                $param['jie_dt'] = '';
                $param['jie_sufee'] = '';
                $param['jie_shuo'] = '';
            }

            $param['state'] = max($param['jindu']);
            $param['jindu'] = implode(',', $param['jindu']);
            $types = $param['type'] == 1 ? 'ZS' : 'YS';
            $param['number'] = (new LegalService())->create_legal_number($types);
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                $id = LegalModel::insertGetId($param);

                (new OptService())->relation_file($files,'legal',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'legal', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'legal', $id, '案件登记');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['type'] = (new LegalService())->get_legal_type();
            $data['zhuang'] = (new OptService())->get_base_info('fa_legal_zhuang');
            $data['state'] = (new LegalService())->get_legal_state();
            $data['year'] = (new LegalService())->get_legal_year();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_legal(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['type'])){
                return outputJson(4000, '请选择案件类型');
            }
            if(empty($param['year'])){
                return outputJson(4000, '请选择案件年度');
            }
            if(empty($param['cause'])){
                return outputJson(4000, '请输入案由');
            }
            if(empty($param['she_unit'])){
                return outputJson(4000, '请选择涉案单位');
            }
            if(empty($param['gao_yuan'])){
                return outputJson(4000, '请输入原告');
            }
            if(empty($param['gao_bei'])){
                return outputJson(4000, '请输入被告');
            }
            if(empty($param['biaofee'])){
                return outputJson(4000, '请输入诉讼标的');
            }
            if(empty($param['zhuang'])){
                return outputJson(4000, '请选择案件状态');
            }
            if(!isset($param['jindu'])){
                return outputJson(4000, '请选择案件进度');
            }

            if(!in_array(3, $param['jindu'])){
                $param['bei_su'] = '';
                $param['bei_ce'] = '';
            }
            if(!in_array(6, $param['jindu'])){
                $param['li_dt'] = '';
                $param['li_shou'] = '';
                $param['li_sufee'] = '';
                $param['li_miao'] = '';
            }
            if(!in_array(9, $param['jindu'])){
                $param['yi_number'] = '';
                $param['yi_shen'] = '';
                $param['yi_dt'] = '';
                $param['yi_sufee'] = '';
                $param['yi_an'] = '';
                $param['yi_pan'] = '';
            }
            if(!in_array(12, $param['jindu'])){
                $param['er_number'] = '';
                $param['er_shen'] = '';
                $param['er_dt'] = '';
                $param['er_sufee'] = '';
                $param['er_an'] = '';
                $param['er_pan'] = '';
            }
            if(!in_array(15, $param['jindu'])){
                $param['zhi_number'] = '';
                $param['zhi_yuan'] = '';
                $param['zhi_dt'] = '';
                $param['zhi_shuo'] = '';
            }
            if(!in_array(18, $param['jindu'])){
                $param['jie_dt'] = '';
                $param['jie_sufee'] = '';
                $param['jie_shuo'] = '';
            }

            $param['state'] = max($param['jindu']);
            $param['jindu'] = implode(',', $param['jindu']);


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);


            Db::startTrans();
            try {
                LegalModel::update($param);


                (new OptService())->relation_file($files,'legal',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'legal', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'legal', $param['id'], '案件登记');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LegalModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['she_unit_info'] = (new UserService())->get_users_part_arr($data['data']['she_unit'], 'dept');
            $data['engin_info'] = EnginModel::get_info_by_pk($data['data']['engin']);

            $data['type'] = (new LegalService())->get_legal_type();
            $data['zhuang'] = (new OptService())->get_base_info('fa_legal_zhuang');
            $data['state'] = (new LegalService())->get_legal_state();
            $data['jinduArr'] = explode(',', $data['data']['jindu']);
            $data['year'] = (new LegalService())->get_legal_year();
            $data['files'] = (new OptService())->get_file('legal', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_legal(Request $request){
        $param = $request->post();
        $data = LegalModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            LegalModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'legal', $data['id'], '案件登记');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_legal(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = LegalModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['she_unit_name'] = DeptModel::get_name_by_pk($data['data']['she_unit']);
        $data['engin_info'] = EnginModel::get_info_by_pk($data['data']['engin']);

        $data['state'] = (new LegalService())->get_legal_state();
        $data['jinduArr'] = explode(',', $data['data']['jindu']);
        $data['data']['type_name'] = (new LegalService())->get_legal_type_name($data['data']['type']);
        $data['data']['zhuang'] = (new OptService())->get_base_showname('fa_legal_zhuang', $data['data']['zhuang']);
        $data['data']['state_name'] = (new LegalService())->get_legal_state_name($data['data']['state']);
        $data['files'] = (new OptService())->get_file('legal', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_legal_jin(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['le.deleted_user','=', 0];
        $where[] = ['le.comid','=',$this->userinfo['comid']];

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['le.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['le.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['le.type','=', $param['type']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['le.year','like', '%'.$param['year'].'%'];
        }
        if(isset($param['cause']) && !empty($param['cause'])){
            $where[] = ['le.cause','like', '%'.$param['cause'].'%'];
        }
        if(isset($param['she_unit']) && !empty($param['she_unit'])){
            $where[] = ['le.she_unit','=', $param['she_unit']];
        }
        if(isset($param['engin']) && !empty($param['engin'])){
            $where[] = ['engin.name','like', '%'.$param['engin'].'%'];
        }

        $data['type'] = (new LegalService())->get_legal_type();
        $data['is_type'] = isset($param['type']) ? $param['type'] : '';


        $data['she_unit'] = (new UserService())->get_chaninfo_all();;
        $data['is_she_unit'] = isset($param['she_unit']) ? $param['she_unit'] : '';

        $whereYi = [];
        $whereEr = [];
        $fendt = $request->param('fendt', 1, 'int');
        if($fendt == 1){
            $start = date('Y-m-d');
            $end = date('Y-m-d', (time()+7*24*3600));
            $whereYi[] = ['le.yi_dt','between', [$start, $end]];
            $whereEr[] = ['le.er_dt','between', [$start, $end]];
        }
        if($fendt == 2){
            $start = date('Y-m-d');
            $end = date('Y-m-d', (time()+30*24*3600));
            $whereYi[] = ['le.yi_dt','between', [$start, $end]];
            $whereEr[] = ['le.er_dt','between', [$start, $end]];
        }
        if($fendt == 3){
            $start = date('Y-m-01');
            $end = date('Y-m-d', strtotime("$start +1 month -1 day"));
            $whereYi[] = ['le.yi_dt','between', [$start, $end]];
            $whereEr[] = ['le.er_dt','between', [$start, $end]];
        }

        $list = [];
        $dataYi = LegalModel::alias('le')
            ->field('le.*,engin.name as engin_name,she.name as she_unit_name')
            ->leftJoin('dept she','she.id=le.she_unit')
            ->leftJoin('engin engin','engin.id=le.engin')
            ->where($where)->where($whereYi)->order('le.id DESC')->select()->toArray();
        foreach ($dataYi as $item){
            $item['kaitingdt'] = $item['yi_dt'];
            $item['type_name'] = (new LegalService())->get_legal_type_name($item['type']);
            $list[] = $item;
        }

        $dataEr = LegalModel::alias('le')
            ->field('le.*,engin.name as engin_name,she.name as she_unit_name')
            ->leftJoin('dept she','she.id=le.she_unit')
            ->leftJoin('engin engin','engin.id=le.engin')
            ->where($where)->where($whereEr)->order('le.id DESC')->select()->toArray();
        foreach ($dataEr as $item){
            $item['kaitingdt'] = $item['er_dt'];
            $item['type_name'] = (new LegalService())->get_legal_type_name($item['type']);
            $list[] = $item;
        }


        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'),$data);
    }







}
